<?php
declare(strict_types=1);
namespace cms\exam\api_controller;

use cms\exam\service\UserBindService;
use cms\exam\service\UserService;
use Hyperf\Di\Annotation\Inject;
use Hyperf\HttpServer\Annotation\Controller;
use Hyperf\HttpServer\Annotation\RequestMapping;
use Hyperf\Utils\Context;
use HyperfExt\Auth\Contracts\AuthManagerInterface;
use Psr\Http\Message\ResponseInterface;

/**
 * Class UserController
 * @package cms\exam\api_controller
 * @Controller(prefix="api/exam/user")
 */
class UserController extends CommonController
{
    /**
     * @Inject()
     * @var UserService
     */
    protected UserService $userService;

    /**
     * @Inject()
     * @var UserBindService
     */
    protected UserBindService $userBindService;

    /**
     * @return ResponseInterface
     * @RequestMapping(path="info")
     */
    public function info(): ResponseInterface
    {
        $app = $this->_getApp(true);
        $client_type = Context::get('client_type', '');
        if (in_array($client_type, ['wechat_mobile', 'wechat_computer'])) {
            $wechat_user = make(AuthManagerInterface::class)->guard('wechat')->user();
            if ($wechat_user) {
                $user_bind = $this->userBindService->model->where([
                    ['app_id', '=', $app->id],
                    ['bind_type', '=', 'wechat'],
                    ['bind_id', '=', $wechat_user->id]
                ])->first();
                if ($user_bind) {
                    return $this->success('请求成功', $user_bind->user);
                }
            }
        }
        return $this->error('请求失败');
    }

    public function detail(): ResponseInterface
    {
        $id = (int) $this->request->input('id', 0);
    }
}